package com.waimai.app.qa.support;

import java.awt.image.BufferedImage;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.openqa.selenium.WebDriver;

/**
 * ImageDiff Class
 * @author AiHui
 */
public class ImageDiff {

	public static String screenShot(WebDriver augmentedDriver, String folderName, String imageName) {
		/*Robot rb = null;
		try {
			rb = new Robot();
		} catch (AWTException e) {
			e.printStackTrace();
		}
		rb.mouseMove(0, 0);// move mouse to top left corner, for eliminating some effects of hover in pages

		String dirName = null;
		if (Settings.screenShotType == 1) {
			dirName = Settings.sampleImagePath + folderName;
		} else if (Settings.screenShotType == 2) {
			dirName = Settings.contrastImagePath + folderName;
		} else {
			System.err.println("Wrong type！Please check type.properties！");
		}
		augmentedDriver = new Augmenter().augment(augmentedDriver);
		try {
			File sourceFile = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE);
			FileUtils.copyFile(sourceFile, new File(dirName + File.separator + imageName + ".png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return dirName + File.separator + imageName + ".png";*/
		
		return null;
	}
	
	public static String[][] getPX(InputStream in) { 
        int[] rgb = new int[3]; 
   
        BufferedImage bi = null; 
        try { 
            bi = ImageIO.read(in); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
   
        int width = bi.getWidth(); 
        int height = bi.getHeight(); 
        int minx = bi.getMinX(); 
        int miny = bi.getMinY(); 
        String[][] list = new String[width][height]; 
        for (int i = minx; i < width; i++) { 
            for (int j = miny; j < height; j++) { 
                int pixel = bi.getRGB(i, j); 
                rgb[0] = (pixel & 0xff0000) >> 16; 
                rgb[1] = (pixel & 0xff00) >> 8; 
                rgb[2] = (pixel & 0xff); 
                list[i][j] = rgb[0] + "," + rgb[1] + "," + rgb[2]; 
            } 
        } 
        rgb = null; 
        return list; 
    } 
	
	public static boolean compareImage(InputStream img1, InputStream img2) { 
        //分析图片相似度 begin 
        String[][] list1 = getPX(img1); 
        String[][] list2 = getPX(img2); 
        int xiangsi = 0; 
        int busi = 0; 
        int i = 0, j = 0; 
        for (String[] strings : list1) { 
            if ((i + 1) == list1.length) { 
                continue; 
            } 
            for (int m = 0; m < strings.length; m++) { 
                try { 
                    String[] value1 = list1[i][j].toString().split(","); 
                    String[] value2 = list2[i][j].toString().split(","); 
                    int k = 0; 
                    for (int n = 0; n < value2.length; n++) { 
                        if (Math.abs(Integer.parseInt(value1[k]) 
                                - Integer.parseInt(value2[k])) < 5) { 
                            xiangsi++; 
                        } else { 
                            busi++; 
                        } 
                    } 
                } catch (RuntimeException e) { 
                    continue; 
                } 
                j++; 
            } 
            i++; 
        } 
   
        list1 = list2; 
        list2 = list1; 
        i = 0; 
        j = 0; 
        for (String[] strings : list1) { 
            if ((i + 1) == list1.length) { 
                continue; 
            } 
            for (int m = 0; m < strings.length; m++) { 
                try { 
                    String[] value1 = list1[i][j].toString().split(","); 
                    String[] value2 = list2[i][j].toString().split(","); 
                    int k = 0; 
                    for (int n = 0; n < value2.length; n++) { 
                        if (Math.abs(Integer.parseInt(value1[k]) 
                                - Integer.parseInt(value2[k])) < 5) { 
                            xiangsi++; 
                        } else { 
                            busi++; 
                        } 
                    } 
                } catch (RuntimeException e) { 
                    continue; 
                } 
                j++; 
            } 
            i++; 
        } 
        String baifen = ""; 
        try { 
            baifen = ((Double.parseDouble(xiangsi + "") / Double 
                    .parseDouble((busi + xiangsi) + "")) + ""); 
            baifen = baifen.substring(baifen.indexOf(".") + 1, baifen 
                    .indexOf(".") + 3); 
        } catch (Exception e) { 
            baifen = "0"; 
        } 
        if (baifen.length() <= 0) { 
            baifen = "0"; 
        } 
        if (busi == 0) { 
            baifen = "100"; 
        } 
           
        list1 = null; 
        list2 = null; 
           
        if (!baifen.equals("100")) { 
            return false; 
        } else { 
            return true; 
        } 
    }
	
	/*public static void process(WebDriver driver, String folderName, String checkPoint, String xpath) throws Exception {

        WebElement element=driver.findElement(By.xpath(xpath));
		String sample = checkPoint + "_sample";

		String actual = null;
		if (Settings.screenShotType == 1) {
			actual = screenShot(driver, folderName, sample);
		} else if (Settings.screenShotType == 2) {
			actual = screenShot(driver, folderName, checkPoint);
			File samplefile=new File(Settings.sampleImagePath + folderName + File.separator + sample+".png");
			File actualfile=new File(actual);
			InputStream sampleImage= new FileInputStream(samplefile);
			InputStream actualImage = new FileInputStream(actualfile);
			
			if(compareImage(sampleImage, actualImage)==true){
				System.out.println("图片一致----pass");
				//logger.info("图片一致pass");	
			}
			else
				System.out.println("图片不一致----fail");
				//logger.error("两张图片不一致，网页有变化");
		} else {
			System.err.println("Wrong type！Please check type.properties！");
		}
	}*/
}
